package com.nengchuan.eic.nceconomy.util;

import com.nengchuan.config.exception.NcException;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author cailei
 * @create 2020-04-15 11:38
 */

public class TrimUtil<T> {


    /**
     * 将对象中的参数进行去空格
     *
     * @param t
     * @return
     */
    public static <T> T trimObject(T t) {
        if (t == null) {
            return null;
        }
        Field[] fields = t.getClass().getDeclaredFields();
        for (Field field : fields) {
            Class<?> type = field.getType();
            if ("class java.lang.String".equals(type.toString())) {
                String fieldName = field.getName();
                Object value = getValueByFieldName(fieldName, t);
                if (value != null) {
                    doSetValue(t, type, fieldName, value.toString().trim());//去除前后端空格
                    //doSetValue(obj, type, fieldName, value.toString().replace(" ", ""));//去除所有空格
                    if ("null".equals(value.toString().trim()) || "NULL".equals(value.toString().trim())
                            || "Null".equals(value.toString().trim())) {
                        throw new NcException("对象字符串属性值不能为null、NULL、Null");
                    }
                    if(isSpecialChar(value.toString().trim())){
                        throw new NcException("对象字符串属性值不能有特殊字符$^&*_+{}<>");
                    }
                }
            }

        }
        return t;

    }

    /**
     * 根据属性名获取该类此属性的值
     *
     * @param fieldName
     * @param object
     * @return
     */
    private static Object getValueByFieldName(String fieldName, Object object) {
        String firstLetter = fieldName.substring(0, 1).toUpperCase();
        String getter = "get" + firstLetter + fieldName.substring(1);
        try {
            Method method = object.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(object, new Object[]{});
            return value;
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * 将处理过的值放入对象中
     *
     * @param obj
     * @param classType
     * @param fieldName
     * @param value
     */
    private static void doSetValue(Object obj, Class<?> classType, String fieldName, String value) {
        String firstLetter = fieldName.substring(0, 1).toUpperCase();
        String setter = "set" + firstLetter + fieldName.substring(1);
        try {
            Method method = obj.getClass().getMethod(setter, new Class[]{classType});
            method.invoke(obj, new Object[]{value});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断是否含有特殊字符
     *
     * @param str
     * @return true为包含，false为不包含
     */
    public static boolean isSpecialChar(String str) {
        String regEx = "[$^&*_+{}<>]|\n|\r|\t";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

}
